﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace DevExpress.MailClient.Win.Controls
{
    public partial class ucMyLockedFiles : BaseControl
    {
        public ucMyLockedFiles()
        {
            InitializeComponent();
            //Load document types
            try
            {
                PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DOCUMENT_TYPESLoadAll().ToList().ForEach(new Action<PRO_DOCS.DAL.Database.proc_ARCHIVE_DOCUMENT_TYPESLoadAllResult>(delegate(PRO_DOCS.DAL.Database.proc_ARCHIVE_DOCUMENT_TYPESLoadAllResult _dOCUMENT_TYPE)
                {
                    CheckedListBoxItem listViewItem = new CheckedListBoxItem();
                    listViewItem.Value = _dOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_ID.ToString();
                    listViewItem.Description = _dOCUMENT_TYPE.ARCHIVE_DOCUMENT_TYPE_NAME;
                    lstDocumentTypes.Items.Add(listViewItem);
                }));
            }
            catch { }

        }

        frmMain CURRENT_MAIN_FORM
        {
            get { return (frmMain)this.ParentForm; }
        }


        private void optAllMyLockedFiles_CheckedChanged(object sender, EventArgs e)
        {
            if (optAllMyLockedFiles.Checked)
            {
                CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION = frmMain.MyLockedOptions.AllMyLockedFiles;
                CURRENT_MAIN_FORM.GET_MY_LOCKED_FILES(CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION, new List<decimal>(), DateTime.Now, DateTime.Now);
            }
        }

        private void optSpecificDocumentTypes_CheckedChanged(object sender, EventArgs e)
        {
            lstDocumentTypes.Visible = optSpecificDocumentTypes.Checked;
            tblMyLockedFilesSelections.Update();

            if (optSpecificDocumentTypes.Checked)
            {
                CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION = frmMain.MyLockedOptions.SpeceficDocumentType;
            }
        }

        private void optSpeceficDate_CheckedChanged(object sender, EventArgs e)
        {
            dtNvgtrSpecificDate.Visible = optSpeceficDate.Checked;
            tblMyLockedFilesSelections.Update();

            if (optSpeceficDate.Checked)
            {
                CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION = frmMain.MyLockedOptions.SpeceficDate;

                CURRENT_MAIN_FORM.GET_MY_LOCKED_FILES(CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION, new List<decimal>(), dtNvgtrSpecificDate.DateTime, dtNvgtrSpecificDate.DateTime);
            }
        }

        private void optBetweenDates_CheckedChanged(object sender, EventArgs e)
        {
            tblDateRange.Visible = optBetweenDates.Checked;
            tblMyLockedFilesSelections.Update();
            if (optBetweenDates.Checked)
            {
                CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION = frmMain.MyLockedOptions.BetweenDates;
                CURRENT_MAIN_FORM.GET_MY_LOCKED_FILES(CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION, new List<decimal>(), dtNvgtrFromDate.DateTime.Date, dtNvgtrToDate.DateTime.Date);
            }
        }

        private void ucMyLockedFiles_Load(object sender, EventArgs e)
        {
            optAllMyLockedFiles.Checked = true;
            CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION = frmMain.MyLockedOptions.AllMyLockedFiles;
            CURRENT_MAIN_FORM.GET_MY_LOCKED_FILES(CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION, new List<decimal>(), DateTime.Now, DateTime.Now);
        }

        private void lstDocumentTypes_ItemCheck(object sender, XtraEditors.Controls.ItemCheckEventArgs e)
        {
            List<decimal> _selectedDocumentTypes = new List<decimal>();
            for (int i = 0; i < lstDocumentTypes.Items.Count; i++)
            {
                if (lstDocumentTypes.GetItemChecked(i))
                {
                    _selectedDocumentTypes.Add(Convert.ToInt64(lstDocumentTypes.GetDisplayItemValue(i).ToString()));
                }
            }


            CURRENT_MAIN_FORM.GET_MY_LOCKED_FILES(CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION, _selectedDocumentTypes, DateTime.Now, DateTime.Now);
        }

        private void dtNvgtrSpecificDate_EditValueChanged(object sender, EventArgs e)
        {
            CURRENT_MAIN_FORM.GET_MY_LOCKED_FILES(CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION, new List<decimal>(), dtNvgtrSpecificDate.DateTime, dtNvgtrSpecificDate.DateTime);
        }

        private void dtNvgtrFromToDate_DateTimeChanged(object sender, EventArgs e)
        {

        }

        private void dtNvgtrFromToDate_EditValueChanged(object sender, EventArgs e)
        {
            dxErrorProvider1.ClearErrors();
            bool valid_dates = false;

            valid_dates = !string.IsNullOrEmpty(dtNvgtrToDate.Text) && !string.IsNullOrEmpty(dtNvgtrFromDate.Text);

            if (valid_dates)
            {
                CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION = frmMain.MyLockedOptions.BetweenDates;
                valid_dates = dtNvgtrFromDate.DateTime <= dtNvgtrToDate.DateTime;
                if (valid_dates)
                {
                    CURRENT_MAIN_FORM.GET_MY_LOCKED_FILES(CURRENT_MAIN_FORM.CURRENT_SELECTED_OPTION, new List<decimal>(), dtNvgtrFromDate.DateTime, dtNvgtrToDate.DateTime);
                }
                else
                {
                    dxErrorProvider1.SetError(dtNvgtrFromDate, "From date must be lessa than or equals the to date", XtraEditors.DXErrorProvider.ErrorType.Information);
                }
            }
            else
            {
                if (string.IsNullOrEmpty(dtNvgtrToDate.Text))
                {
                    dxErrorProvider1.SetError(dtNvgtrToDate, PRO_DOCS.BLL.WIN.Properties.ErrorProviderMessages.Default.InputLeftBlank.Replace("{name}", "To date"), XtraEditors.DXErrorProvider.ErrorType.Information);
                }
                if (string.IsNullOrEmpty(dtNvgtrFromDate.Text))
                {
                    dxErrorProvider1.SetError(dtNvgtrFromDate, PRO_DOCS.BLL.WIN.Properties.ErrorProviderMessages.Default.InputLeftBlank.Replace("{name}", "From date"), XtraEditors.DXErrorProvider.ErrorType.Information);
                }
            }
        }
    }
}
